// pages/joinTheGroup/joinTheGroup.js
const orderService = require('../../service/orderService.js')
const utils = require('../../utils/util.js');
// load promise 
require('../../utils/wx-pro.js');
var timer = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xcxGoodsId: '',
    OpenCode: '',
    isJoin: '',
    openGroupList: [],
    xcxGoods: '',
    noPtImg: [],
    ptCount: '',
    remain: '',
    remainFormat: '',
    showBtn: true,
    shopName: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      xcxGoodsId: options.xcxGoodsId,
      OpenCode: options.OpenCode
    })
  },

  onShow: function (){
    this.getOpenGroupList()
  },

  setTimer() {
    timer = setInterval(() => {
      if (this.data.remain > 1000) {
        this.setData({
          remain: this.data.remain - 1000,
          remainFormat: this.secondToDate(this.data.remain / 1000),
          showBtn: true
        })
      } else {
        this.setData({
          remainFormat: '',
          showBtn: false
        })
      }
    }, 1000)
  },
  secondToDate(result) {
    var h = Math.floor(result / 3600);
    var m = Math.floor((result / 60 % 60));
    var s = Math.floor((result % 60));
    if (h > 0) {
      return result = h + "小时" + m + "分钟" + s + "秒";
    } else {
      return result = m + "分钟" + s + "秒";
    }
  },
  clearTimer() {
    timer = clearInterval(timer)
  },

  getOpenGroupList(){
    var params = { xcxGoodsId: this.data.xcxGoodsId, OpenCode: this.data.OpenCode }
    orderService.xcxOpenGroupList(params).then(data => {
      if (data.code == 200) {
        this.setData({
          isJoin: data.response.isJoin,
          openGroupList: data.response.openGroupList,
          xcxGoods: data.response.xcxGoods,
          remain: data.response.openGroupList[0].endTime - data.timestamp,
          shopName: data.response.shopName
        })
        this.clearTimer()
        this.setTimer()
        var i = this.data.xcxGoods.ptType - this.data.openGroupList.length
        this.setData({
          ptCount: i
        })
        console.log(i)
        var list = []
        for(var a=0;a<i;a++){
          list.push('img')
        }
        this.setData({
          noPtImg: list
        })
      } else {
        this.showToastFuc(data.message)
      }
    })
      .catch((e) => {
        console.log(e)
      })
  },
  goGroupConfirm(){
    var _this = this
    wx.showModal({
      title: '提示',
      content: '该拼团由【' + _this.data.shopName + '】店发布，配送半径为【' + _this.data.shopName +'】店半径三公里内，特殊商品（限自提）除外。',
      confirmText: '去参团',
      cancelText: '取消',
      success: function (res) {
        if (res.confirm) {
          var goodsInfo = _this.data.xcxGoods
          var goods = { num: 1, buyType: 3, goods: goodsInfo, groupId: _this.data.openGroupList[0].groupId }
          wx.setStorageSync('orderGoods', JSON.stringify([goods]))
          wx.navigateTo({
            url: '../orderConfirm/orderConfirm'
          })
        }
      }
    })
  },
  /**
   * 用户点击按钮分享
   */
  onShareAppMessage: function () {
    return {
      title: '【' + this.data.shopName +'】' + this.data.xcxGoods.name,
      path: '/pages/joinTheGroup/joinTheGroup?xcxGoodsId=' + this.data.xcxGoodsId + '&OpenCode=' + this.data.OpenCode
    }
  }
})